package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.EDiscountInfoLog;
import com.ruoyi.system.domain.EPerfectLog;
import com.ruoyi.system.domain.ETaskLog;
import com.ruoyi.system.service.IEDiscountInfoLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.EDiscountLogMapper;
import com.ruoyi.system.domain.EDiscountLog;
import com.ruoyi.system.service.IEDiscountLogService;

/**
 * 贴现人完善任务记录Service业务层处理
 *
 * @author ruoyi
 * @date 2022-04-21
 */
@Service
public class EDiscountLogServiceImpl implements IEDiscountLogService
{
    @Autowired
    private EDiscountLogMapper eDiscountLogMapper;
    @Autowired
    IEDiscountInfoLogService ieDiscountInfoLogService;

    /**
     * 查询贴现人完善任务记录
     *
     * @param id 贴现人完善任务记录主键
     * @return 贴现人完善任务记录
     */
    @Override
    public EDiscountLog selectEDiscountLogById(Long id)
    {
        return eDiscountLogMapper.selectEDiscountLogById(id);
    }

    /**
     * 查询贴现人完善任务记录列表
     *
     * @param eDiscountLog 贴现人完善任务记录
     * @return 贴现人完善任务记录
     */
    @Override
    @DataScope()
    public List<EDiscountLog> selectEDiscountLogList(EDiscountLog eDiscountLog)
    {
        return eDiscountLogMapper.selectEDiscountLogList(eDiscountLog);
    }

    /**
     * 新增贴现人完善任务记录
     *
     * @param eDiscountLog 贴现人完善任务记录
     * @return 结果
     */
    @Override
    public int insertEDiscountLog(EDiscountLog eDiscountLog)
    {
        eDiscountLog.setCreateTime(DateUtils.getNowDate());
        return eDiscountLogMapper.insertEDiscountLog(eDiscountLog);
    }

    /**
     * 修改贴现人完善任务记录
     *
     * @param eDiscountLog 贴现人完善任务记录
     * @return 结果
     */
    @Override
    public int updateEDiscountLog(EDiscountLog eDiscountLog)
    {
        return eDiscountLogMapper.updateEDiscountLog(eDiscountLog);
    }

    /**
     * 批量删除贴现人完善任务记录
     *
     * @param ids 需要删除的贴现人完善任务记录主键
     * @return 结果
     */
    @Override
    public int deleteEDiscountLogByIds(Long[] ids)
    {
        return eDiscountLogMapper.deleteEDiscountLogByIds(ids);
    }

    /**
     * 删除贴现人完善任务记录信息
     *
     * @param id 贴现人完善任务记录主键
     * @return 结果
     */
    @Override
    public int deleteEDiscountLogById(Long id)
    {
        return eDiscountLogMapper.deleteEDiscountLogById(id);
    }

    @Override
    public List<EDiscountInfoLog> exportDiscountLog(Long discountId) {
        EDiscountInfoLog eDiscountInfoLog=new EDiscountInfoLog();
        eDiscountInfoLog.setDiscountId(discountId);
        return ieDiscountInfoLogService.selectEDiscountInfoLogList(eDiscountInfoLog);
    }

    @Override
    @Async
    public void importEDiscountInfoLog(List<EDiscountInfoLog> eDiscountInfoLogs, String operName, LoginUser loginUser) {
        EDiscountLog eDiscountLogIns=new EDiscountLog();
        eDiscountLogIns.setCreateBy(operName);
        eDiscountLogIns.setUserId(loginUser.getUserId());
        eDiscountLogIns.setDeptId(loginUser.getDeptId());
        eDiscountLogIns.setDiscountState("UNFINISHED");//未完成
        insertEDiscountLog(eDiscountLogIns);
        Long discountId=eDiscountLogIns.getId();
        ieDiscountInfoLogService.importEDiscountInfoLog(discountId,eDiscountInfoLogs,operName, loginUser);
        Boolean is=ieDiscountInfoLogService.isUnfinished(discountId);
        if(!is){
            EDiscountLog eDiscountLogUp=new EDiscountLog();
            eDiscountLogUp.setId(discountId);
            eDiscountLogUp.setDiscountState("COMPLETED");//已完成
            updateEDiscountLog(eDiscountLogUp);
        }
    }
}
